package com.ruoyi.ucar.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.ProductDetailQueryBoReq;
import com.ruoyi.ucar.boreq.ProductListQueryBoReq;
import com.ruoyi.ucar.boresp.*;
import com.ruoyi.ucar.domain.po.ProductDetailQueryPo;
import com.ruoyi.ucar.domain.po.ProductMobileListPo;
import com.ruoyi.ucar.domain.po.ProductQueryListPo;
import com.ruoyi.ucar.mapper.TProductMapper;
import com.ruoyi.ucar.moreq.ProductDetailQueryMoReq;
import com.ruoyi.ucar.moreq.ProductListQueryMoReq;
import com.ruoyi.ucar.moreq.ProductMobileListQueryMoReq;
import com.ruoyi.ucar.service.ISysFileMainService;
import com.ruoyi.ucar.service.ITProductService;
import com.ruoyi.ucar.domain.TProduct;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.voreq.ProductAddVoReq;
import com.ruoyi.ucar.voreq.ProductUpdateVoReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品Service业务层处理
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Service
public class TProductServiceImpl extends ServiceImpl<TProductMapper, TProduct> implements ITProductService
{
    @Autowired
    private TProductMapper tProductMapper;

    @Autowired
    private ISysFileMainService iSysFileMainService;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 查询产品
     * 
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public TProduct selectTProductById(Long id)
    {
        return tProductMapper.selectTProductById(id);
    }

    /**
     * 查询产品列表
     * 
     * @param tProduct 产品
     * @return 产品
     */
    @Override
    public List<TProduct> selectTProductList(TProduct tProduct)
    {
        return tProductMapper.selectTProductList(tProduct);
    }

    /**
     * 新增产品
     * 
     * @param tProduct 产品
     * @return 结果
     */
    @Override
    public int insertTProduct(TProduct tProduct)
    {
        return tProductMapper.insertTProduct(tProduct);
    }

    /**
     * 修改产品
     * 
     * @param tProduct 产品
     * @return 结果
     */
    @Override
    public int updateTProduct(TProduct tProduct)
    {
        return tProductMapper.updateTProduct(tProduct);
    }

    /**
     * 批量删除产品
     * 
     * @param ids 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteTProductByIds(Long[] ids)
    {
        return tProductMapper.deleteTProductByIds(ids);
    }

    /**
     * 删除产品信息
     * 
     * @param id 产品主键
     * @return 结果
     */
    @Override
    public int deleteTProductById(Long id)
    {
        return tProductMapper.deleteTProductById(id);
    }

    @Override
    public  ProductMobileListBoResp mobileList() {
        ProductMobileListQueryMoReq productMobileListQueryMoReq=new ProductMobileListQueryMoReq();
        productMobileListQueryMoReq.setProductStatus(1l);
        productMobileListQueryMoReq.setDeleteFlag(1l);
        List<ProductMobileListPo>  productMobileListPoList=tProductMapper.mobileList(productMobileListQueryMoReq);
        ProductMobileListBoResp  productMobileListBoResp=new ProductMobileListBoResp();
        if(StringUtils.isNotNull(productMobileListPoList)&&productMobileListPoList.size()>0){
            List<ProductMobileListBo> productMobileListBoList=new ArrayList<>();
            productMobileListPoList.forEach(productMobileListPo-> {
                ProductMobileListBo productMobileListBo= CommonUtil.objToObj(productMobileListPo,ProductMobileListBo.class);
                productMobileListBo.setProductStatusDict(DictUtils.getDictLabel("product_status",String.valueOf(productMobileListPo.getProductStatus())));
                productMobileListBo.setProductTypeDict(DictUtils.getDictLabel("product_type",productMobileListPo.getProductType()));
                productMobileListBoList.add(productMobileListBo);
            });
            productMobileListBoResp.setProductMobileListBos(productMobileListBoList);
            return productMobileListBoResp;
        }else{
            return null;
        }
    }

    @Override
    public ProductListQueryBoResp queryProductByCond(ProductListQueryBoReq productListQueryBoReq) {
        ProductListQueryBoResp productListQueryBoResp = new ProductListQueryBoResp();
        Page<ProductQueryListPo>  page=new Page<>(productListQueryBoReq.getPageIndex(),productListQueryBoReq.getPageSize());
        ProductListQueryMoReq productListQueryMoReq=CommonUtil.objToObj(productListQueryBoReq,ProductListQueryMoReq.class);
        productListQueryMoReq.setDeleteFlag(1l);
        IPage<ProductQueryListPo> productQueryListPoIPage= tProductMapper.productListQueryPage(page,productListQueryMoReq);
        if(StringUtils.isNotNull(productQueryListPoIPage.getRecords())&&productQueryListPoIPage.getRecords().size()>0){
            productListQueryBoResp.setTotalCount(new Integer(String.valueOf(productQueryListPoIPage.getTotal())));
            productListQueryBoResp.setTotalPage(new Integer(String.valueOf(productQueryListPoIPage.getPages())));
            List<ProductListQueryBo>  productListQueryBos=new ArrayList<>();
            productQueryListPoIPage.getRecords().forEach(productQueryListPo -> {
                ProductListQueryBo productListQueryBo=CommonUtil.objToObj(productQueryListPo,ProductListQueryBo.class);
                productListQueryBo.setProductStatusDict(DictUtils.getDictLabel("product_status",String.valueOf(productQueryListPo.getProductStatus())));
                productListQueryBo.setProductTypeDict(DictUtils.getDictLabel("product_type",productQueryListPo.getProductType()));
                productListQueryBos.add(productListQueryBo);
            });
            productListQueryBoResp.setProductListQueryBos(productListQueryBos);
            logger.info("产品查询BO端返回结果：{}"+productListQueryBoResp);
        }else{
            return null;
        }
        return productListQueryBoResp;
    }


    @Transactional
    @Override
    public String saveProduct(ProductAddVoReq productAddVoReq) throws IOException {
            TProduct tProduct=CommonUtil.objToObj(productAddVoReq,TProduct.class);
            String productUuid=CommonUtil.createUuid();
            String sysFileMainUuid=iSysFileMainService.saveSysFile(productAddVoReq.getFile());
            tProduct.setFileMainUuid(sysFileMainUuid);
            tProduct.setProductUuid(productUuid);
            tProduct.setDeleteFlag(1L);
            tProduct.setCreatedBy(SecurityUtils.getUsername());
            tProductMapper.insert(tProduct);
            return productUuid;
    }

    @Override
    public ProductDetailQueryBoResp productDetailQuery(ProductDetailQueryBoReq productDetailQueryBoReq) {
           ProductDetailQueryMoReq  productDetailQueryMoReq=CommonUtil.objToObj(productDetailQueryBoReq,ProductDetailQueryMoReq.class)  ;
           productDetailQueryMoReq.setDeleteFlag(1l);
           List<ProductDetailQueryPo> productDetailQueryPos=tProductMapper.productDetailQuery(productDetailQueryMoReq);
           if(StringUtils.isNotNull(productDetailQueryPos)&&productDetailQueryPos.size()>0){
               ProductDetailQueryPo productDetailQueryPo=productDetailQueryPos.get(0);
               ProductDetailQueryBoResp productDetailQueryBoResp=CommonUtil.objToObj(productDetailQueryPo,ProductDetailQueryBoResp.class);
               productDetailQueryBoResp.setProductStatusDict(DictUtils.getDictLabel("product_status",String.valueOf(productDetailQueryPo.getProductStatus())));
               productDetailQueryBoResp.setProductTypeDict(DictUtils.getDictLabel("product_type",productDetailQueryPo.getProductType()));
               return productDetailQueryBoResp;
           }else{
            return null;
          }
    }



    @Override
    public int updateProduct(ProductUpdateVoReq productUpdateVoReq) throws IOException {
        TProduct tProduct=this.selectProductByProductUuid(productUpdateVoReq.getProductUuid());
        tProduct.setProductName(productUpdateVoReq.getProductName());
        tProduct.setProductPrice(productUpdateVoReq.getProductPrice());
        tProduct.setProductPurchasingPrice(productUpdateVoReq.getProductPurchasingPrice());
        tProduct.setProductRealPrice(productUpdateVoReq.getProductRealPrice());
        tProduct.setProductReturnPrice(productUpdateVoReq.getProductReturnPrice());
        tProduct.setProductAgencyFee(productUpdateVoReq.getProductAgencyFee());
        tProduct.setProductStatus(productUpdateVoReq.getProductStatus());
        tProduct.setProductType(productUpdateVoReq.getProductType());
        tProduct.setRemark(productUpdateVoReq.getRemark());
        tProduct.setFixFlag(productUpdateVoReq.getFixFlag());
        tProduct.setReturnPriceDays(productUpdateVoReq.getReturnPriceDays());
        tProduct.setStoreNumber(productUpdateVoReq.getStoreNumber());
        tProduct.setTotalNumber(productUpdateVoReq.getTotalNumber());
        tProduct.setUsedNumber(productUpdateVoReq.getUsedNumber());
        tProduct.setSortNumber(productUpdateVoReq.getSortNumber());
        tProduct.setSecondAgencyFeeRatio(productUpdateVoReq.getSecondAgencyFeeRatio());
        if(StringUtils.isNotEmpty(productUpdateVoReq.getFileMainUuId())){
            tProduct.setDeleteFlag(1L);
            tProduct.setUpdatedBy(SecurityUtils.getUsername());
        }else {
            String sysFileMainUuid = iSysFileMainService.saveSysFile(productUpdateVoReq.getFile());
            tProduct.setFileMainUuid(sysFileMainUuid);
            tProduct.setDeleteFlag(1L);
            tProduct.setUpdatedBy(SecurityUtils.getUsername());
        }
         return tProductMapper.update(tProduct,new LambdaQueryWrapper<TProduct>().eq(TProduct::getProductUuid,productUpdateVoReq.getProductUuid()).eq(TProduct::getDeleteFlag,1l));
    }

    @Override
    public TProduct selectProductByProductUuid(String productUuid ) {
        return this.tProductMapper.selectOne(new LambdaQueryWrapper<TProduct>().eq(TProduct::getProductUuid,productUuid));
    }

    @Override
    public int deleteTProductById(String productUuid, Long id) {
        TProduct tProduct=null;
        if(StringUtils.isNotEmpty(productUuid)) {
            tProduct = this.selectProductByProductUuid(productUuid);
            tProduct.setDeleteFlag(-1l);
            tProduct.setUpdatedBy(SecurityUtils.getUsername());
            return tProductMapper.update(tProduct,new LambdaQueryWrapper<TProduct>().eq(TProduct::getProductUuid, productUuid));
        }else{
            tProduct = this.getById(id);
            tProduct.setDeleteFlag(-1l);
            tProduct.setUpdatedBy(SecurityUtils.getUsername());
            return tProductMapper.update(tProduct,new LambdaQueryWrapper<TProduct>().eq(TProduct::getId, id));
        }
    }

    @Override
    public int batchDelete(String[] productUuids, Long[] ids) {
        if(StringUtils.isNotNull(productUuids)&&productUuids.length>0) {
            return tProductMapper.deleteTProductByUuids(-1l, productUuids);
        }else{
            return tProductMapper.deleteTProductByids(-1l, ids);
        }
    }
}
